﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class NeedforYearDetailWindow : EFViewCmes
    {
       // private EIBlock measureBlock;
        public NeedforYearDetailWindow(string _dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(_dialogType);
          //  measureBlock = InitDate
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.needDetailWindows.GetChildObjects<EFDataField>(null, true);
            _view.Args.Add("fdDepotId", this.fdStockPlanId.GetDataValue().ToString());
            base.ViewProperty = _view;
        }
        #region 初始化页面数据
        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();
            switch (type)
            {
                case "insert":
                    this.fdMaterialType.Value = "有编码物资";
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    this.fdMaterialType.Value = "";
                    this.fdMaterialType.IsEnabled = false;
                    this.fdMaterialType.eRegex = null;
                    this.fdMaterialType.eValidateErrorPrompt = null;
                    this.fdMaterialType.Label = "物资类型";

                    this.fdMaterialName.IsReadOnly = true;

                    this.fdMaterialCode.IsEnabled = false;
                    this.fdMaterialCode.eRegex = null;
                    this.fdMaterialCode.eValidateErrorPrompt = null;
                    this.fdMaterialCode.Label = "物资编码";

                    this.fdQuality.IsReadOnly = true;
                    this.fdSpecModel.IsReadOnly = true;
                    this.fdNumber.IsReadOnly = true;
                    this.fdMetric.IsEnabled = false;
                    base.Mode = OperationMode.Update;
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 按钮操作
        /// <summary>
        /// 新增按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Commit_Click(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick("ZMCMD2", sender, e);
        }
        /// <summary>
        /// 返回、取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Calcel_Click(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        #endregion

        #region customview 值获取
        /// <summary>
        /// Customview 取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdMaterialCode_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null && e.OldValue.Equals(""))
            {
                return;
            }
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker != null)
            {
                if (picker.ItemObject != null)
                {

                    if (!picker.ItemObject.Equals(""))
                    {
                        this.fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdMaterialId").GetValue(picker.ItemObject, null);
                        this.fdMaterialName.Value = picker.ItemObject.GetType().GetProperty("fdMaterialName").GetValue(picker.ItemObject, null);
                        this.fdMaterialCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialCode").GetValue(picker.ItemObject, null);
                        this.fdQuality.Value = picker.ItemObject.GetType().GetProperty("fdQuality").GetValue(picker.ItemObject, null);
                        this.fdSpecModel.Value = picker.ItemObject.GetType().GetProperty("fdSpecModel").GetValue(picker.ItemObject, null);
                        this.fdNumber.Value = picker.ItemObject.GetType().GetProperty("fdNumber").GetValue(picker.ItemObject, null);
                        this.fdMaterialInnerCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialInnerCode").GetValue(picker.ItemObject, null);
                        this.fdMetric.Value = picker.ItemObject.GetType().GetProperty("fdMetric").GetValue(picker.ItemObject, null);
                        this.fdStockNum.Value = picker.ItemObject.GetType().GetProperty("fdStockNum").GetValue(picker.ItemObject, null);
                        this.fdSafeBaseNum.Value = picker.ItemObject.GetType().GetProperty("fdSafeBaseNum").GetValue(picker.ItemObject, null);
                        this.fdPrice.Value = picker.ItemObject.GetType().GetProperty("fdPrice").GetValue(picker.ItemObject, null);
                        this.fdSupplyName.Value = picker.ItemObject.GetType().GetProperty("fdSupplyName").GetValue(picker.ItemObject, null);
                    }
                    else
                    {
                        this.fdMaterialId.Value = "";
                        this.fdMaterialName.Value = "";
                        this.fdMaterialCode.Value = "";
                        this.fdQuality.Value = "";
                        this.fdSpecModel.Value = "";
                        this.fdNumber.Value = "";
                        this.fdMaterialInnerCode.Value = "";
                        this.fdMetric.Value = "";
                        this.fdStockNum.Value = "";
                        this.fdSafeBaseNum.Value = "";
                        this.fdPrice.Value = "";
                        this.fdSupplyName.Value = "";
                    }
                }
            }
        }

        #endregion

        private void fdMaterialType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            if (e.NewValue.Equals("无编码物资"))
            {
                //物资编码
                this.fdMaterialCode.eLabel = "物资编码";
                this.fdMaterialCode.IsEnabled = false;
                (this.fdMaterialCode.Content as CmesCustomView).Display = "";
                (this.fdMaterialCode.Content as CmesCustomView).CleanPopupContent();
                this.fdMaterialCode.eRegex = null;
                this.fdMaterialCode.eValidateErrorPrompt = null;
                this.fdMaterialCode.Value = "";

                //物资名称
                this.fdMaterialName.Value = "";
                this.fdMaterialName.eLabel = "*物资名称";
                this.fdMaterialName.eRegex = @"^.{1,40}$";
                this.fdMaterialName.eValidateErrorPrompt = "输入1-40个字符";
                this.fdMaterialName.IsReadOnly = false;

                //规格型号
                this.fdSpecModel.Value = "";
                this.fdSpecModel.eLabel = "*规格型号";
                this.fdSpecModel.eRegex = "^.{1,50}$";
                this.fdSpecModel.eValidateErrorPrompt = "输入1-50个字符";
                this.fdSpecModel.IsReadOnly = false;
                //材质
                this.fdQuality.Value = "";
                this.fdQuality.eRegex = "^.{0,50}$";
                this.fdQuality.eValidateErrorPrompt = "输入0-50个字符";
                this.fdQuality.IsReadOnly = false;
                //计量单位
                this.fdMetric.Value = "";
                this.fdMetric.eLabel = "*计量单位";
                this.fdMetric.eRegex = "^.+$";
                this.fdMetric.eValidateErrorPrompt = "不能为空";
                this.fdMetric.IsEnabled = true;
                //单价
                this.fdPrice.Value = "";
                this.fdPrice.eLabel = "*单价(元)";
                this.fdPrice.eRegex = "((^\\d{1,6}$)|(^\\d{1,6}\\.\\d{1,5}$)|(^0\\.\\d{1,5}$))";
                this.fdPrice.eValidateErrorPrompt = "输入整数位不超过6位，小数位不超过5位的数字";
                this.fdPrice.IsReadOnly = false;
                //图号
                this.fdNumber.Value = "";
                this.fdNumber.eRegex = "^.{0,50}$";
                this.fdNumber.eValidateErrorPrompt = "输入0-50个字符";
                this.fdNumber.IsReadOnly = false;

                this.fdStockNum.Value = "0";
                this.fdSafeBaseNum.Value = "0";
                this.fdSupplyName.Value = "";
                //隐藏值
                this.fdMaterialId.Value = "na";
                this.fdMaterialInnerCode.Value = "";
            }
            else if (e.NewValue.Equals("有编码物资"))
            {
                //物资编码
                this.fdMaterialCode.eBindingName = "result-0-fdMaterialCode";
                this.fdMaterialCode.eLabel = "*物资编码";
                this.fdMaterialCode.eRegex = "^.+$";
                this.fdMaterialCode.eValidateErrorPrompt = "不能为空";
                (this.fdMaterialCode.Content as CmesCustomView).Display = "";
                (this.fdMaterialCode.Content as CmesCustomView).CleanPopupContent();
                this.fdMaterialCode.Value = "";
                this.fdMaterialCode.IsEnabled = true;

                //物资名称
                this.fdMaterialName.eLabel = "物资名称";
                this.fdMaterialName.eRegex = null;
                this.fdMaterialName.eValidateErrorPrompt = null;
                this.fdMaterialName.Value = "";
                this.fdMaterialName.IsReadOnly = true;

                //规格型号
                this.fdSpecModel.Value = "";
                this.fdSpecModel.eLabel = "规格型号";
                this.fdSpecModel.eRegex = null;
                this.fdSpecModel.eValidateErrorPrompt = null;
                this.fdSpecModel.IsReadOnly = true;

                //材质
                this.fdQuality.Value = "";
                this.fdQuality.IsReadOnly = true;

                //计量单位
                this.fdMetric.eLabel = "计量单位";
                this.fdMetric.eRegex = null;
                this.fdMetric.eValidateErrorPrompt = null;
                this.fdMetric.Value = "";
                this.fdMetric.Validate();
                this.fdMetric.IsEnabled = false;

                //单价
                this.fdPrice.eLabel = "单价(元)";
                this.fdPrice.eRegex = null;
                this.fdPrice.eValidateErrorPrompt = null;
                this.fdPrice.Value = "";
                this.fdPrice.IsReadOnly = true;

                //图号
                this.fdNumber.Value = "";
                this.fdNumber.IsReadOnly = true;

                //隐藏值
                this.fdMaterialId.Value = "";
                this.fdMaterialInnerCode.Value = "";
            }
        }
    }
}
